﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Reflection;
using System.Data.OracleClient;
using System.Data.OleDb;

namespace WinFormClient
{
    public partial class frmMain : Form
    {
        private Type[] mytypes;
        private List<KeyValuePair<string, string>> mappingList = new List<KeyValuePair<string, string>>();
        public frmMain()
        {
            InitializeComponent();
        }

        private void bt_load_Click(object sender, EventArgs e)
        {
            #region Class
            cbNameSpace.Items.Clear();
            
            Assembly a = Assembly.Load("K.EnterpriseLibary");//加载指定的实体类库
            mytypes = a.GetTypes();//获取全部的类
            List<string> listNameSpace = new List<string>();
            foreach (Type t in mytypes)
            {
                if (t.Namespace != null)
                {
                    listNameSpace.Add(t.Namespace);
                }
            }
            IEnumerable<string> disListNameSpace = listNameSpace.Distinct<string>();

            //显示命名空间

            foreach (string nameSpace in disListNameSpace)
            {
                cbNameSpace.Items.Add(nameSpace);
            }
            #endregion

            #region Relation


            cbDB.Items.Add("LIS2");


            


            #endregion


        }


        private void cbNameSpace_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbClass.Items.Clear();
            if (cbNameSpace.SelectedItem.ToString() != "==Select NameSpace==")
            {
                foreach (Type t in mytypes)
                {
                    if (t.Namespace != null && t.Namespace.ToString() == cbNameSpace.SelectedItem.ToString())
                    {
                        //cbClass.Items.Add(t.ToString().Substring(t.Namespace.Length + 1));
                        cbClass.Items.Add(t.ToString());
                    }
                }                
            }

        }

        private void lb_ClassMethod_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



        private void cbClass_SelectedIndexChanged(object sender, EventArgs e)
        {

            cbProperty.Items.Clear();
            if (cbClass.SelectedItem.ToString() != "==Select Class==")
            {
                Assembly a = Assembly.Load("K.EnterpriseLibary");
                Type t = a.GetType(cbClass.SelectedItem.ToString());

                BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
                BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

                //MethodInfo[] mi = t.GetMethods(flags);
                //FieldInfo[] fi = t.GetFields();
                PropertyInfo[] pi = t.GetProperties(flags);
                //Object obj = Activator.CreateInstance(t);
                foreach (PropertyInfo p in pi)
                {
                    cbProperty.Items.Add(p.Name);
                }
            }

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Maximized;
            /*
            cbNameSpace.Items.Add("==Select NameSpace==");
            cbNameSpace.SelectedIndex = 0;

            cbClass.Items.Add("==Select Class==");
            cbClass.SelectedIndex = 0;

            cbProperty.Items.Add("==Select Property==");
            cbProperty.SelectedIndex = 0;
            */
        }

        private void cbTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbColumn.Items.Clear();
            string connConnectString = "Data Source=HUB25.MITACAD.COM;Persist Security Info=True;User ID=LIS2;Password=LIS2;Unicode=True;Provider=msdaora;";
            //string connConnectString = "server=XL-20101023GDGN;uid=jusns;pwd=123456;database=jusnsv20;Pooling=true;Provider=SQLOLEDB;";
            OleDbConnection conn = new OleDbConnection(connConnectString);
            string sqlAllTable = "select column_name from user_tab_columns where table_name = '" + cbTable.SelectedItem.ToString() + "'";
            //string sqlAllTable = "select name from sys.all_columns where object_id = (select object_id from sys.all_objects where type= 'U' and name = '" + cbTable.SelectedItem.ToString() + "')";
            OleDbCommand comm = new OleDbCommand(sqlAllTable,conn);
            OleDbDataAdapter dap = new OleDbDataAdapter(comm);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ListItem listItem = new ListItem(dr[0].ToString(), dr[0].ToString());
                cbColumn.Items.Add(listItem);
            }

            cbColumn.ValueMember = "Value";
            cbColumn.DisplayMember = "Key";
        }

        private void cbDB_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbTable.Items.Clear();

            string connConnectString = "Data Source=HUB25.MITACAD.COM;Persist Security Info=True;User ID=LIS2;Password=LIS2;Unicode=True;Provider=msdaora;";
            //string connConnectString = "server=XL-20101023GDGN;uid=jusns;pwd=123456;database=jusnsv20;Pooling=true;Provider=SQLOLEDB;";
            OleDbConnection conn = new OleDbConnection(connConnectString);
            string sqlAllTable = "select table_name from all_tables where owner = '" + cbDB.SelectedItem.ToString() + "'";
            //string sqlAllTable = "select * from sys.all_objects where type = 'U'";
            OleDbCommand comm = new OleDbCommand(sqlAllTable,conn);
            OleDbDataAdapter dap = new OleDbDataAdapter(comm);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ListItem listItem = new ListItem(dr[0].ToString(), dr[0].ToString());
                cbTable.Items.Add(listItem);
            }

            cbTable.ValueMember = "Value";
            cbTable.DisplayMember = "Key";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            KeyValuePair<string, string> mappingKeyValuePair = new KeyValuePair<string, string>(cbProperty.SelectedItem.ToString(),cbColumn.SelectedItem.ToString());
            listBox1.Items.Add(mappingKeyValuePair);
            mappingList.Add(mappingKeyValuePair);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            Oracle orcl = new Oracle();
            orcl.ConnectionString = "";
            /*
            必要参数：1、命名空间；2、类名；3、属性与字段组合值
             */
            MapXML2 mapXml2 = new MapXML2();
            mapXml2.NameSpace = cbClass.SelectedItem.ToString().Substring(cbNameSpace.SelectedItem.ToString().Length + 1);
            mapXml2.ClassName = cbClass.SelectedItem.ToString().Substring(cbNameSpace.SelectedItem.ToString().Length+1);
            mapXml2.TableName = cbTable.SelectedItem.ToString();
            mapXml2.ListMapping = mappingList;
            mapXml2.Assembly = "K.EnterpriseLibary.dll";
            mapXml2.Domain = "mapping";
            mapXml2.ClassType = cbClass.SelectedItem.ToString();
            
            mapXml2.Builder(true, true, true, true, true, true, true, true, true, true, true);

         }
    }

    public class ListItem
    {
        public string Key { get; set; }
        public string Value { get; set; }


        public ListItem(string pKey, string pValue)
        {
            this.Key = pKey;
            this.Value = pValue;
        }
        public override string ToString()
        {
            return this.Key;
        }
    }

}
